﻿using PBO.Data;
using PBO.Game;
using PokemonBattle.BattleNetwork;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

namespace PBO.Game.BattleClass
{
    public partial class Pokemon
    {

        /// <summary>
        /// 混乱状态
        /// </summary>
        public bool confuse(bool moveCall, Pokemon target = null)
        {
            if (GetSelTrait() == Trait.OwnTempo)
                return false;
            if (MyTeam.Group.Safeguard)
                return false;
            if (BattleState.confusion)
                return false;
            BattleState.confusion = true;
            if (moveCall)
            {
                BattleState.confusionTurn = 1;
            }
            else
            {
                BattleState.confusionTurn = MyTeam.random.Next(1, 5);
            }
            BattleState.confusionCounter = 0;
            MyTeam.log.AppendMsg("Confuse", this.GetNameString());
            //MyTeam.log.AppendText(GetNameString() + "混乱了！");
            if (GetItem() == Item.PersimBerry || GetItem() == Item.LumBerry)
                RaiseItem();
            return true;
        }

        /// <summary>
        /// 害怕状态
        /// </summary>
        public bool terrify(Pokemon target)
        {
            if (GetSelTrait() == Trait.InnerFocus && !(target.GetSelTrait() == Trait.MoldBreaker))
                return false;
            if (BattleState.moved)
                return false;
            BattleState.afraid = true;
            //MyTeam.log.AppendMsg("Flinch", this.GetNameString());
            //MyTeam.log.AppendText(GetNameString() + "害怕了！");
            if (GetSelTrait() == Trait.Steadfast)
            {
                AbilityLvUpChanged(StatType.Speed, 1, false);
                //SpeedLVUp(1, false);
            }
            return true;
        }

        /// <summary>
        /// 魅惑状态
        /// </summary>
        public bool Captivate(Pokemon target, bool traitEff = false)
        {
            if (GetSelTrait() == Trait.Oblivious && target.GetSelTrait() != Trait.MoldBreaker)
                return false;
            if (BattleState.captivated)
                return false;
            if (MyTeam.Group.Safeguard)
                return false;
            if (this.Gender != PokemonGender.None && target.Gender != PokemonGender.None && this.Gender != target.Gender)
            {
                BattleState.captivated = true;
                BattleState.captivateTarget = target;
                if (traitEff)
                {
                    MyTeam.log.AppendMsg("EnAttract", this.GetNameString());
                    //MyTeam.log.AppendText(target.GetNameString() + "的魅惑之身特性使" + CustomName + "着迷了！");
                }
                else
                {
                    MyTeam.log.AppendMsg("EnAttract", this.GetNameString());
                    //MyTeam.log.AppendText(GetNameString() + "着迷了！");
                }
                if (GetItem() == Item.MentalHerb || GetItem() == Item.LumBerry)
                {
                    RaiseItem();
                }
                else if (GetItem() == Item.DestinyKnot && target.Captivate(this))
                {
                    MyTeam.log.AppendMsg("ItemEnAttract", target.GetNameString(), Item.DestinyKnot);
                    //MyTeam.log.AppendText(CustomName + "的红色线团使" + target.GetNameString() + "也着迷了！");
                    UseItem();
                }
                return true;
            }
            return false;
        }

        /// <summary>
        /// 哈欠
        /// </summary>
        public bool BeYawned()
        {
            if (State == PokemonState.No && !BattleState.yawn && !MyTeam.Group.Safeguard)
            {
                BattleState.yawn = true;
                BattleState.yawnCounter = 0;
                MyTeam.log.AppendMsg("EnYawn", this.GetNameString());
                //MyTeam.log.AddText(GetNameString() + "昏昏欲睡！");
                return true;
            }
            return false;
        }

        /// <summary>
        /// 寄生种子
        /// </summary>
        public bool BeSeeded(PokemonIndex target)
        {
            if (this.BattleState.seed || this.HaveType(BattleType.Grass))
            {
                MyTeam.log.AppendMsg("Fail0");
                //MyTeam.log.AddText(GetNameString() + "已经被种下了种子");
            }
            else
            {
                BattleState.seed = true;
                BattleState.seedTarget = target;
                MyTeam.log.AppendMsg("EnLeechSeed", this.GetNameString());
                //MyTeam.log.AddText(GetNameString() + "被种下了种子！");
            }
            return this.BattleState.seed;
        }

        /// <summary>
        /// 鼓掌状态
        /// </summary>
        public bool Encore()
        {
            if (LastMoveIsValid && LastMove.PP != 0)
            {
                if (BattleState.encore == false)
                {
                    BattleState.encore = true;
                    BattleState.encoreTurn = Battle.EncoreTurn;
                    BattleState.encoreCounter = 0;
                    MyTeam.log.AppendMsg("EnEncore", this.GetNameString());
                    //MyTeam.log.AddText(GetNameString() + "被鼓掌了！");
                    if (GetItem() == Item.MentalHerb)
                    {
                        RaiseItem();
                    }
                }
                else
                {
                    MyTeam.log.AppendMsg("Fail0");
                    //MyTeam.log.AddText(GetNameString() + "已经处于鼓掌状态");
                }
                return true;
            }
            return false;
        }

        /// <summary>
        /// 挑拨状态
        /// </summary>
        public bool Taunt()
        {
            if (BattleState.taunt == false)
            {
                BattleState.taunt = true;
                BattleState.TauntTurn = Battle.TauntTurn;
                BattleState.TauntCounter = 0;
                MyTeam.log.AppendMsg("EnTaunt", this.GetNameString());
                //MyTeam.log.AddText(GetNameString() + "被挑拨了！");
                if (GetItem() == Item.MentalHerb)
                {
                    RaiseItem();
                }
            }
            else
            {
                MyTeam.log.AppendMsg("Fail0");
                //MyTeam.log.AddText(GetNameString() + "已经处于挑拨状态");
            }
            return true;
        }

        /// <summary>
        /// 潜水
        /// </summary>
        public bool Dive(bool prepareCount, Move move)
        {
            if (prepareCount)
            {
                BattleState.dive = false;
                BattleState.nextMove = null;
                if (ImageChanged != null)
                {
                    ImageChanged(this, EventArgs.Empty);
                }
            }
            else if (GetItem() == Item.PowerHerb)
            {
                RaiseItem();
            }
            else
            {
                BattleState.dive = true;
                BattleState.nextMove = move;
                MyTeam.log.AppendMsg("Prepare291", this.GetNameString());
                //MyTeam.log.AddText(GetNameString() + "潜入了水中！");
                if (ImageChanged != null)
                {
                    ImageChanged(this, EventArgs.Empty);
                }
                return false;
            }
            return true;
        }

        /// <summary>
        /// 钻地
        /// </summary>
        public bool Dig(bool prepareCount, Move move)
        {
            if (prepareCount)
            {
                BattleState.dig = false;
                BattleState.nextMove = null;
                if (ImageChanged != null)
                {
                    ImageChanged(this, EventArgs.Empty);
                }
            }
            else if (GetItem() == Item.PowerHerb)
            {
                RaiseItem();
            }
            else
            {
                BattleState.dig = true;
                BattleState.nextMove = move;
                MyTeam.log.AppendMsg("Prepare91", this.GetNameString());
                //MyTeam.log.AddText(GetNameString() + "钻入了地下！");
                if (ImageChanged != null)
                {
                    ImageChanged(this, EventArgs.Empty);
                }
                return false;
            }
            return true;
        }

        /// <summary>
        /// 飞天
        /// </summary>
        public bool Fly(bool prepareCount, Move move)
        {
            if (!prepareCount && MyTeam.ground.Gravity)
            {
                MyTeam.log.AppendMsg("GravityCantUseMove", this.GetNameString(), move.Data.Name);
                //MyTeam.log.AddText(GetNameString() + "因为重力的作用无法飞起");
                return false;
            }
            if (prepareCount)
            {
                BattleState.fly = false;
                BattleState.nextMove = null;
                if (ImageChanged != null)
                {
                    ImageChanged(this, EventArgs.Empty);
                }
                if (MyTeam.ground.Gravity)
                {
                    MyTeam.log.AppendMsg("Fail0");
                    //MyTeam.log.AddText("但它失败了");
                    return false;
                }
            }
            else if (GetItem() == Item.PowerHerb)
            {
                RaiseItem();
            }
            else
            {
                BattleState.fly = true;
                BattleState.nextMove = move;
                MyTeam.log.AppendMsg("Prepare19", this.GetNameString());
                //MyTeam.log.AddText(GetNameString() + "飞上了天空！");
                if (ImageChanged != null)
                {
                    ImageChanged(this, EventArgs.Empty);
                }
                return false;
            }
            return true;
        }

        /// <summary>
        /// 飞跳
        /// </summary>
        public bool Jump(bool prepareCount, Move move)
        {
            if (!prepareCount && MyTeam.ground.Gravity)
            {
                MyTeam.log.AppendMsg("GravityCantUseMove", this.GetNameString(), move.Data.Name);
                //MyTeam.log.AddText(GetNameString() + "因为重力的作用无法跳起");
                return false;
            }
            if (prepareCount)
            {
                BattleState.jump = false;
                BattleState.nextMove = null;
                if (ImageChanged != null)
                {
                    ImageChanged(this, EventArgs.Empty);
                }
                if (MyTeam.ground.Gravity)
                {
                    MyTeam.log.AppendMsg("Fail0");
                    //MyTeam.log.AddText("但它失败了");
                    return false;
                }
            }
            else if (GetItem() == Item.PowerHerb)
            {
                RaiseItem();
            }
            else
            {
                BattleState.jump = true;
                BattleState.nextMove = move;
                MyTeam.log.AppendMsg("Prepare340", this.GetNameString());
                //MyTeam.log.AddText(GetNameString() + "跳起了！");
                if (ImageChanged != null)
                {
                    ImageChanged(this, EventArgs.Empty);
                }
                return false;
            }
            return true;
        }

        /// <summary>
        /// 消失(影遁)
        /// </summary>
        public bool Disappear(bool prepareCount, Move move)
        {
            if (prepareCount)
            {
                BattleState.disappear = false;
                BattleState.nextMove = null;
                if (ImageChanged != null)
                {
                    ImageChanged(this, EventArgs.Empty);
                }
            }
            else if (GetItem() == Item.PowerHerb)
            {
                RaiseItem();
            }
            else
            {
                BattleState.disappear = true;
                BattleState.nextMove = move;
                MyTeam.log.AppendMsg("Prepare467", this.GetNameString());
                //MyTeam.log.AddText(GetNameString() + "消失了！");
                if (ImageChanged != null)
                {
                    ImageChanged(this, EventArgs.Empty);
                }
                return false;
            }
            return true;
        }


        //public bool BeTelekinesis()
        //{
        //    if (BattleState.telekinesis)
        //    {
        //        return false;
        //    }
        //    else
        //    {
        //        BattleState.telekinesis = true;
        //        BattleState.telekinesisCounter = 0;
        //    }
        //}


        /// <summary>
        /// 回合结束
        /// </summary>
        public void CountOfEndTurn(Random random, Battle battle)
        {
            OnEndTurn();
            switch (GetSelTrait())
            {
                case Trait.SolarPower:
                case Trait.IceBody:
                case Trait.RainDish:
                    RaiseTrait();
                    break;
                case Trait.DrySkin:
                    if (MyTeam.ground.Weather == Weather.Sunny)
                    {
                        MyTeam.log.AppendMsg("Hurt", this.GetNameString());
                        //MyTeam.log.AppendText(GetNameString() + "因为晴天而受到伤害");
                        this.HurtFraction(16);
                    }
                    else if (MyTeam.ground.Weather == Weather.Rainy && HealthPower != MaxHp)
                    {
                        MyTeam.log.AppendMsg("HpRecover", this.GetNameString());
                        //MyTeam.log.AppendText(GetNameString() + "的干燥皮肤特性使它回复了HP");
                        this.HPRecoverFraction(16);
                    }
                    break;
                case Trait.ShedSkin:
                    RaiseTrait();
                    break;
                case Trait.SpeedBoost:
                    RaiseTrait();
                    break;
                case Trait.Hydration:
                    RaiseTrait();
                    break;
                case Trait.SlowStart:
                    if (BattleState.slowStartCounter != 0)
                    {
                        BattleState.slowStartCounter -= 1;
                        if (BattleState.slowStartCounter == 0)
                        {
                            MyTeam.log.AppendMsg("DeSlowStart", this.GetNameString());
                            //MyTeam.log.AddText(GetNameString() + "成功启动了！");
                        }
                    }
                    break;
                case Trait.Truant:
                    if (BattleState.stayOneTurn)
                        BattleState.traitRaised = !BattleState.traitRaised;
                    break;
            }
            if (HealthPower == 0)
                return;

            if (!IsFullHp)
            {
                if (BattleState.aquaRing)
                {
                    MyTeam.log.AppendMsg("AquaRing", this.GetNameString());
                    //MyTeam.log.AddText(GetNameString() + "从液体中得到了营养！");
                    if (GetItem() == Item.BigRoot)
                    {
                        HPRecover(MaxHp * 0.0625 * 1.3);
                    }
                    else
                    {
                        this.HPRecoverFraction(16);
                    }
                }
                if (BattleState.ingrain)
                {
                    MyTeam.log.AppendMsg("Ingrain", this.GetNameString());
                    //MyTeam.log.AddText(GetNameString() + "的根从地面中得到了营养！");
                    if (GetItem() == Item.BigRoot)
                    {
                        HPRecover(MaxHp * 0.0625 * 1.3);
                    }
                    else
                    {
                        this.HPRecoverFraction(16);
                    }
                }
            }

            if (GetItem() == Item.Leftovers && HealthPower != MaxHp)
            {
                MyTeam.log.AppendMsg("ItemRecover2", this.GetNameString(), Item.Leftovers);
                //MyTeam.log.AddText(GetNameString() + "的剩饭使它回复了HP！");
                this.HPRecoverFraction(16);
            }
            else if (GetItem() == Item.BlackSludge && this.HaveType(BattleType.Poison) && HealthPower != MaxHp)
            {
                MyTeam.log.AppendMsg("ItemRecover2", this.GetNameString(), Item.BlackSludge);
                //MyTeam.log.AddText(GetNameString() + "的黑色淤泥使它回复了HP！");
                this.HPRecoverFraction(16);
            }
            else if (GetItem() == Item.LeppaBerry)
            {
                RaiseItem();
            }

            if (GetSelTrait() != Trait.MagicGuard)
            {
                if (BattleState.seed)
                {
                    Pokemon target = battle.GetPokemonFromIndex(BattleState.seedTarget);
                    if (HealthPower != 0)
                    {
                        double damage = 0;
                        if (HealthPower > Math.Truncate(MaxHp * 0.125))
                        {
                            damage = MaxHp * 0.125;
                        }
                        else
                        {
                            damage = HealthPower;
                        }
                        MyTeam.log.AppendMsg("LeechSeed", this.GetNameString());
                        //MyTeam.log.AddText(GetNameString() + "被种子吸取了HP！");
                        GetHurt(damage);
                        if (GetSelTrait() == Trait.LiquidOoze)
                        {
                            MyTeam.log.AppendMsg("Ability", this.GetNameString(), this.GetSelTrait());
                            MyTeam.log.AppendMsg("Hurt", target.GetNameString());
                            //MyTeam.log.AppendText(GetNameString() + "的淤泥特性使" + target.GetNameString() + "受到了伤害！");
                            target.GetHurt(damage);
                        }
                        else
                        {
                            if (target.GetItem() == Item.BigRoot)
                            {
                                target.HPRecover(damage * 1.3);
                            }
                            else
                            {
                                target.HPRecover(damage);
                            }
                            MyTeam.log.AppendMsg("HpRecover", this.GetNameString());
                            //MyTeam.log.AddText(target.GetNameString() + "回复了HP！");
                        }
                        if (HealthPower == 0)
                            return;
                    }
                }

                if (GetItem() == Item.StickyBarb)
                {

                    MyTeam.log.AppendMsg("ItemHurt", this.GetNameString(), Item.StickyBarb);
                    //MyTeam.log.AddText(GetNameString() + "因为附针而受到了伤害！");
                    this.HurtFraction(8);
                }
                else if (GetItem() == Item.BlackSludge && !this.HaveType(BattleType.Poison))
                {
                    MyTeam.log.AppendMsg("ItemHurt", this.GetNameString(), Item.BlackSludge);
                    //MyTeam.log.AddText(GetNameString() + "因为黑色淤泥而受到了伤害！");
                    this.HurtFraction(8);
                }
                if (HealthPower == 0)
                    return;

                switch (State)
                {
                    case PokemonState.Burn:
                        MyTeam.log.AppendMsg("BRN", this.GetNameString());
                        //MyTeam.log.AddText(GetNameString() + "烧伤了！");
                        if (GetSelTrait() == Trait.Heatproof)
                        {
                            this.HurtFraction(16);
                        }
                        else
                        {
                            this.HurtFraction(8);
                        }
                        break;
                    case PokemonState.Poison:
                        if (GetSelTrait() == Trait.PoisonHeal)
                        {
                            if (HealthPower != MaxHp)
                            {
                                MyTeam.log.AppendMsg("PoisonHeal", this.GetNameString());
                                //MyTeam.log.AddText(GetNameString() + "的毒疗特性使它回复了HP！");
                                this.HPRecoverFraction(8);
                            }
                        }
                        else
                        {
                            MyTeam.log.AppendMsg("PSN", this.GetNameString());
                            //MyTeam.log.AddText(GetNameString() + "受到了毒素的伤害！");
                            this.HurtFraction(8);
                        }
                        break;
                    case PokemonState.Toxin:
                        if (BattleState.toxinCounter > 0)
                        {
                            if (GetSelTrait() == Trait.PoisonHeal)
                            {
                                if (HealthPower != MaxHp)
                                {
                                    MyTeam.log.AppendMsg("PoisonHeal", this.GetNameString());
                                    //MyTeam.log.AddText(GetNameString() + "的毒疗特性使它回复了HP！");
                                    this.HPRecoverFraction(8);
                                }
                            }
                            else
                            {
                                MyTeam.log.AppendMsg("PSN", this.GetNameString());
                                //MyTeam.log.AddText(GetNameString() + "受到了毒素的伤害！");
                                GetHurt(MaxHp * 0.0625 * BattleState.toxinCounter);
                            }
                        }
                        break;
                }
                if (HealthPower == 0)
                    return;

                if (BattleState.nightmare && State != PokemonState.Sleep)
                    BattleState.nightmare = false;
                if (BattleState.nightmare)
                {
                    MyTeam.log.AppendMsg("Nightmare", this.GetNameString());
                    //MyTeam.log.AddText(GetNameString() + "在梦中受到伤害！");
                    this.HurtFraction(4);
                }
                if (HealthPower == 0)
                    return;

                if (State == PokemonState.Sleep && MyTeam.Group.Opponent.SelectedPokemon.Any(p => p.HealthPower > 0 && p.GetSelTrait() == Trait.BadDreams))
                {
                    MyTeam.log.AppendMsg("BadDreams", this.GetNameString());
                    //MyTeam.log.AddText(GetNameString() + "在梦中受到伤害！");
                    this.HurtFraction(8);
                }
                if (HealthPower == 0)
                    return;

                if (BattleState.curse)
                {
                    MyTeam.log.AppendMsg("Curse", this.GetNameString());
                    //MyTeam.log.AddText(GetNameString() + "被诅咒了！");
                    this.HurtFraction(4);
                }
                if (HealthPower == 0)
                    return;

                if (BattleState.constraint && BattleState.constraintTurn != -1)
                {
                    Pokemon pm = battle.GetPokemonFromIndex(BattleState.constraintBy);
                    if (pm != null && pm.HealthPower > 0)
                    {
                        MyTeam.log.AppendText(GetNameString() + "受到了束缚的伤害！");
                        this.HurtFraction(16);
                    }
                }
                if (HealthPower == 0)
                    return;

            }

            if (GetItem() == Item.ToxicOrb)
            {
                SetState(PokemonState.Toxin, null, false, false, true);
            }
            else if (GetItem() == Item.FlameOrb)
            {
                SetState(PokemonState.Burn, null, false, false, true);
            }

            if (BattleState.futureAtk && BattleState.futureCounter == 3)
            {
                var futureDamage = this.BattleState.futureDamage;
                //MyTeam.log.AppendMsg("FSDD", this.GetNameString());
                MyTeam.log.AppendText(GetNameString() + "受到了来自过去的攻击！");
                if (random.NextDouble() < (double)(BattleState.futureAtkAcc / 100))
                {
                    GetHurt((double)futureDamage);
                }
                else
                {
                    MyTeam.log.AppendMsg("Miss", this.GetNameString());
                    //MyTeam.log.AppendText("但是没有命中...");
                }
                this.BattleState.futureAtk = false;
                this.BattleState.futureCounter = 0;
            }
            if (HealthPower == 0)
                return;

            if (BattleState.perishSong)
            {

                MyTeam.log.AppendMsg("PerishSong", this.GetNameString(), BattleState.perishSongCounter);
                //MyTeam.log.AddText(GetNameString() + "的灭亡歌倒数:" + BattleState.perishSongCounter);
                if (BattleState.perishSongCounter == 0)
                {
                    this.HurtFraction(1);
                    BattleState.perishSong = false;
                }
            }
            if (HealthPower == 0)
                return;
            //if (MyTeam.flameSea)
            //{
            //    MyTeam.log.AddText(GetNameString() + "在火海中受到伤害！");
            //    GetHurt(MaxHp * 0.125);
            //}
        }

        /// <summary>
        /// 是否浮游状态
        /// </summary>
        public bool IsAboveGroud
        {
            get { return GetSelTrait() == Trait.Levitate || BattleState.suspension; }
        }
    }
}
